package com.larry.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * @ProjectName HDFSClient
 * @Author larry
 * @Date 11:05 2021/11/5
 * @Description TODO
 *
 * 客户端常用代码
 *  1.获取客户端对象
 *  2.执行相关的操作命令
 *  3.关闭资源
 **/
public class HdfsClient {

    private FileSystem fs;

    /**
     * 初始化
     * @throws Exception
     */
    @Before
    public void init() throws Exception{
        //连接集群nn的地址
        URI uri = new URI("hdfs://192.168.10.102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();

        //设置用户
        String user = "larry";
        //获取客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }

    /**
     * 新建文件夹
     * @throws URISyntaxException
     * @throws IOException
     */
    @Test
    public void testmkdir() throws URISyntaxException, IOException {

        //创建文件夹
        fs.mkdirs(new Path("/xiyou/huaguoshan"));

    }

    /**
     * 关闭资源
     * @throws IOException
     */
    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }

    /**
     * 上传文件
     * @throws IOException
     */
    @Test
    public void testPut() throws IOException {
        /*
        参数解析
        boolean delSrc  删除原始数据
        boolean overwrite  是否覆盖
        Path src  源路径
        Path dst  目标路径
         */
        //fs.copyFromLocalFile(false,false,new Path("D:\\sunwukong.txt"),new Path("/xiyou/huaguoshan"));
        fs.copyFromLocalFile(false,false,new Path("D:\\zhubajie.txt"),new Path("/xiyou/huaguoshan"));
    }

    /**
     * 文件下载
     * @throws IOException
     * boolean delSrc, Path src, Path dst, boolean useRawLocalFileSystem
     */
    @Test
    public void testGet() throws IOException {
        fs.copyToLocalFile(false,new Path("/xiyou/huaguoshan/sunwukong.txt"),new Path("D:\\"),false);
    }

    /**
     * 删除
     * @throws IOException
     */
    @Test
    public void testRm() throws IOException {
        //删除文件
        fs.delete(new Path("/xiyou/huaguoshan/sunwukong.txt"),false);
        //删除空目录
        fs.delete(new Path("/xiyou/huaguoshan"),false);
        //删除费空目录,递归删除
        fs.delete(new Path("/xiyou/huaguoshan"),true);
    }

    /**
     * 重命名和移动
     * @throws IOException
     */
    @Test
    public void testMove() throws IOException {
        fs.rename(new Path("/xiyou/huaguoshan/sunwukong.txt"),new Path("/xiyou/huaguoshan/sunwukong3.txt"));
    }

    /**
     * 获取文件的详细信息
     * @throws IOException
     */
    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
        while (files.hasNext()) {
            LocatedFileStatus next = files.next();
            System.out.println("--------------------------");
            System.out.println(next.getPermission());
            System.out.println(next.getPath().getName());
            BlockLocation[] locations = next.getBlockLocations();
            System.out.println(Arrays.toString(locations));
        }
    }

    /**
     * 判断文件/文件夹
     * @throws IOException
     */
    @Test
    public void testFile() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus status : fileStatuses) {
            if (status.isFile()){
                System.out.println("文件" + status.getPath().getName());
            }else {
                System.out.println(status.getPath());
            }
        }
    }

}
